//
//  LoginViewController.m
//  liangyi
//
//  Created by 张水 on 16/12/13.
//  Copyright © 2016年 yswh. All rights reserved.
//

#import "LoginViewController.h"
#import "AppHeader.h"
#import "FindPwdViewController.h"
#import "RegisteredViewController.h"
@interface LoginViewController ()<UINavigationControllerDelegate>

@property(nonatomic,strong) CustomTextField *userName;

@property(nonatomic,strong) CustomTextField *userPwd;

@end

@implementation LoginViewController

#pragma mark - Life Cycle
- (void)viewDidLoad {
    [super viewDidLoad];
    [self setNav];
    [self setupUI];
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    self.navigationItem.leftBarButtonItem.tintColor = [UIColor whiteColor];
    self.navigationController.navigationBar.translucent = YES;
    [self.navigationController.navigationBar setBackgroundImage:[UIImage new] forBarMetrics:UIBarMetricsDefault];
    self.navigationController.navigationBar.shadowImage = [UIImage new];
}
-(void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    self.navigationController.navigationBar.translucent = NO;
}


#pragma mark - Data & UI

-(void)setNav{
   
//    self.navigationItem.leftBarButtonItem.tintColor = [UIColor whiteColor];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem  alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemStop target:self action:@selector(cancelLoginDidClick)];
}
- (void)setupUI {
//    [self sliderViewController].sliderNavigationController.delegate = self;
    WeakSelf(wk)
    UIImageView *backgroundImageView = [UIImageView new];
    backgroundImageView.image = [UIImage imageNamed:@"login_bg"];
    backgroundImageView.contentMode = UIViewContentModeScaleAspectFill;
    [self.view addSubview:backgroundImageView];
    [backgroundImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.and.left.and.right.bottom.equalTo(wk.view);
    }];
    
    UIImageView *iconImageView = [UIImageView new];
    iconImageView.image = [UIImage imageNamed:@"logo"];
    iconImageView.contentMode = UIViewContentModeScaleAspectFit;
    [self.view addSubview:iconImageView];
    [iconImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(wk.view).offset(40);
        make.centerX.equalTo(wk.view.mas_centerX);
        make.width.height.equalTo(@170);
    }];
    UILabel *userNameLable = [[UILabel alloc]init];
    [self.view addSubview:userNameLable];
    [userNameLable mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(iconImageView.mas_bottom).offset(20);
        make.left.equalTo(self.view).offset(40);
    }];
    userNameLable.text = @"手机号/邮箱";
    userNameLable.textAlignment = NSTextAlignmentLeft;
    userNameLable.textColor = [UIColor whiteColor];
    userNameLable.font = [UIFont systemFontOfSize:17];
    
    self.userName = [CustomTextField new];
    self.userName.text = [UserDefaultsUtlis getValueForKey:@"userPhone"];
    self.userName.textColor = [UIColor whiteColor];
    [self.view addSubview:self.userName];
    [self.userName mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(userNameLable.mas_left);
        make.top.equalTo(userNameLable.mas_bottom).offset(15);
        make.right.equalTo(wk.view).offset(-20);
        make.height.equalTo(@30);
    }];
    UILabel *lineLabel = [[UILabel alloc]init];
    [self.view addSubview:lineLabel];
    [lineLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.userName.mas_bottom);
        make.left.equalTo(self.view).offset(40);
        make.right.equalTo(self.view).offset(-40);
        make.height.equalTo(@2);
    }];
    lineLabel.backgroundColor = [UIColor whiteColor];

    UILabel *userPwdLable = [[UILabel alloc]init];
    [self.view addSubview:userPwdLable];
    [userPwdLable mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.userName.mas_bottom).offset(25);
        make.left.equalTo(self.view).offset(40);
    }];
    userPwdLable.text = @"密码";
    userPwdLable.textAlignment = NSTextAlignmentLeft;
    userPwdLable.textColor = [UIColor whiteColor];
    userPwdLable.font = [UIFont systemFontOfSize:17];
    
    self.userPwd = [CustomTextField new];
    [self.userPwd setSecureTextEntry:YES];
    self.userPwd.textColor = [UIColor whiteColor];
    self.userPwd.validator = [LoginPasswordValidator new];
    [self.view addSubview:self.userPwd];
    [self.userPwd mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(userPwdLable.mas_left);
        make.top.equalTo(userPwdLable.mas_bottom).offset(15);
        make.right.equalTo(wk.view).offset(-20);
        make.height.equalTo(@30);
    }];
    
    UILabel *lineLabel1 = [[UILabel alloc]init];
    [self.view addSubview:lineLabel1];
    [lineLabel1 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.userPwd.mas_bottom);
        make.left.equalTo(self.view).offset(40);
        make.right.equalTo(self.view).offset(-40);
        make.height.equalTo(@2);
    }];
    lineLabel1.backgroundColor = [UIColor whiteColor];
    
//    UIButton *btnRegistBtn = [UIButton new];
//    [btnRegistBtn setBackgroundColor:[UIColor clearColor]];
//    btnRegistBtn.titleLabel.font = [UIFont systemFontOfSize:15];
//    [btnRegistBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
//    [btnRegistBtn setTitle:@"注 册" forState:UIControlStateNormal];
//    [btnRegistBtn setContentMode:UIViewContentModeRight];
//    [btnRegistBtn addTarget:self action:@selector(btnRegistBtnAction) forControlEvents:UIControlEventTouchUpInside];
//    [self.view addSubview:btnRegistBtn];
//    [btnRegistBtn mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.top.equalTo(self.userPwd.mas_bottom).offset(25);
//        make.left.equalTo(self.userPwd);
//        make.width.equalTo(@40);
//        make.height.equalTo(@35);
//    }];

    UIButton *btnForget = [UIButton new];
    [btnForget setBackgroundColor:[UIColor clearColor]];
    btnForget.titleLabel.font = [UIFont systemFontOfSize:15];
    [btnForget setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [btnForget setTitle:@"找回密码?" forState:UIControlStateNormal];
    [btnForget setContentMode:UIViewContentModeRight];
    [btnForget addTarget:self action:@selector(btnForgetAction) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:btnForget];
    [btnForget mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.userPwd.mas_bottom).offset(25);
        make.right.equalTo(self.userPwd).offset(-10);
        make.width.equalTo(@70);
        make.height.equalTo(@35);
    }];
    
    UIButton *btnLogin = [UIButton new];
    btnLogin.layer.cornerRadius = 20.0;
    [btnLogin setBackgroundColor:[UIColor ColorWithHex:@"#5BC4D2"]];
    btnLogin.titleLabel.font = [UIFont systemFontOfSize:18];
    [btnLogin setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [btnLogin setTitle:@"登 录" forState:UIControlStateNormal];
    [btnLogin addTarget:self action:@selector(executeLogin) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:btnLogin];
    [btnLogin mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(btnForget.mas_bottom).offset(60);
        make.left.equalTo(wk.view).offset(60);
        make.right.equalTo(wk.view).offset(-60);
        make.height.equalTo(@50);
    }];
    
}

#pragma mark - Action & Target

-(void)cancelLoginDidClick{
    [self.view endEditing:YES];
    [self dismissViewControllerAnimated:YES completion:nil];
}

//-(void)btnRegistBtnAction{
//    [self.navigationController pushViewController:[RegisteredViewController new] animated:YES];
//}

- (void)btnForgetAction {
    [self.navigationController pushViewController:[FindPwdViewController new] animated:YES];
}

- (void)executeLogin {

    
    NSString *url = [BASEURL stringByAppendingString:kURl_Login];
    
    NSDictionary *dict = @{@"username":self.userName.text,
                           @"password":self.userPwd.text,
                           @"deviceId":ApplicationDelegate.deviceID};
    NSString *jsonString = [NSString dictionaryToJson:dict];
    NSString *AESString = [NSString AES128Encrypt:jsonString];
    NSDictionary *parameters = @{@"parameters":AESString};
    
    [AppNetwork requestWithPath:url parameters:parameters success:^(id responseObject) {
        NSLog(@"%@",responseObject);
        NetworkModel *network = [NetworkModel mj_objectWithKeyValues:responseObject];
        NSInteger code = network.code.integerValue;
        if (code == 0) {
            UserModel *model = [UserModel mj_objectWithKeyValues:network.dataObject];
            NSLog(@"%@",model.id);
            [UserDefaultsUtlis saveValue:model.id forKey:@"userid"];
            [UserDefaultsUtlis saveValue:model.userPhone forKey:@"userPhone"];
            [Sp_MemberStorage insertMemberWithUserModel:model];
            [self cancelLoginDidClick];
        }else {
            [CustomProgressHUD showErrorWithStatus:network.errorDescription];
        }
    } failure:^(id error) {
        [CustomProgressHUD showErrorWithStatus:k_NetWorkStatus_Error ];
    }];
}




@end
